﻿using WMLogistics.Model;
using WMLogistics_Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperExtensions;
using Logistics_Dao;
using Logistics.Model;

namespace WMLogistics.Service
{
    public class WMWuLiuCompanyService
    {
        private WMWuliuCompanyDao dao = new WMWuliuCompanyDao();

        public PageData<wuliu_wm_wlcompany_base> GetList(IPredicate predicate,int page,int pageSize)
        {
            return dao.GetList(predicate, page, pageSize);
        }
        /// <summary>
        /// 通过主键获取物流公司信息
        /// </summary>
        /// <param name="wuliu_id"></param>
        /// <returns></returns>
        public wuliu_wm_wlcompany_base Get(int wuliu_id)
        {
            return dao.Get(wuliu_id);
        }
        /// <summary>
        /// 通过物流编号获取物流公司信息
        /// </summary>
        /// <param name="wuliu_no"></param>
        /// <returns></returns>
         public wuliu_wm_wlcompany_base GetWuliu(string wuliu_no)
        {
            return dao.GetWuliu(wuliu_no);
        }

        public int OpeartWuliuList()
         {
             var list = dao.GetWuliuList();
            foreach(var mod in list)
            {
                wuliu_wm_wlcompany_base enty = new wuliu_wm_wlcompany_base();
               if(mod.city_id!=null)
               {
                   List<string> cityidx = new List<string>();
                   List<string> cityname = new List<string>();
                   int strcityid = (int)mod.city_id;
                   enty.wuliu_id = mod.wuliu_id;
                   cityidx = GetCityBaseID(strcityid, cityidx);
                   enty.city_id_x = string.Join("|", cityidx.ToArray());
                   enty.city_name = string.Join("|", GetCityName(strcityid, cityname).ToArray());
                   if (enty.city_id_x != null)
                   {
                       if (!string.IsNullOrEmpty(enty.city_id_x))
                       {
                           var wuliucityid = enty.city_id_x.Split('|');
                           var wuliucityname = enty.city_name.Split('|');
                           if (wuliucityid.Count() > 0)
                           {
                               if (wuliucityid.Length == 1)
                               {
                                   if (wuliucityid[0] != null)
                                   {
                                       enty.business_scope_id1 = wuliucityid[0];
                                       enty.business_scope_name1 = wuliucityname[0];
                                   }
                                       
                               }
                               if (wuliucityid.Length == 2)
                               {
                                   if (wuliucityid[0] != null)
                                   {
                                       enty.business_scope_id1 = wuliucityid[0];
                                       enty.business_scope_name1 = wuliucityname[0];
                                   }
                                      
                                   if (wuliucityid[1] != null)
                                   {
                                       enty.business_scope_id2 = wuliucityid[1];
                                       enty.business_scope_name2 = wuliucityname[1];
                                   }
                                      
                               }
                               if (wuliucityid.Length == 3)
                               {
                                   if (wuliucityid[0] != null)
                                   {
                                       enty.business_scope_id1 = wuliucityid[0];
                                       enty.business_scope_name1 = wuliucityname[0];
                                   }

                                   if (wuliucityid[1] != null)
                                   {
                                       enty.business_scope_id2 = wuliucityid[1];
                                       enty.business_scope_name2 = wuliucityname[1];
                                   }
                                   if (wuliucityid[2] != null)
                                   {
                                       enty.business_scope_id3 = wuliucityid[2];
                                       enty.business_scope_name3 = wuliucityname[2];
                                   }  
                               }
                               if (wuliucityid.Length == 4)
                               {
                                   if (wuliucityid[0] != null)
                                   {
                                       enty.business_scope_id1 = wuliucityid[0];
                                       enty.business_scope_name1 = wuliucityname[0];
                                   }

                                   if (wuliucityid[1] != null)
                                   {
                                       enty.business_scope_id2 = wuliucityid[1];
                                       enty.business_scope_name2 = wuliucityname[1];
                                   }
                                   if (wuliucityid[2] != null)
                                   {
                                       enty.business_scope_id3 = wuliucityid[2];
                                       enty.business_scope_name3 = wuliucityname[2];
                                   }
                                   if (wuliucityid[3] != null)
                                   {
                                       enty.business_scope_id4 = wuliucityid[3];
                                       enty.business_scope_name4 = wuliucityname[3];
                                   }  
                               }
                           }
                       }
                   }
                 int num=  dao.Update(enty);
               }
            }
            return list.Count();
         }
        CityBaseDao citydao = new CityBaseDao();
        public List<string> GetCityBaseID(int cityid, List<string> cityidx)
        {
            var modcity = citydao.GetCityBase(cityid);
            if (modcity != null)
            {
                if (modcity.city_id > 0)
                {
                    cityidx.Insert(0, modcity.city_id.ToString());
                    int sub_city_id = (int)modcity.sub_city_id;
                    if (sub_city_id == 86)
                    {
                        return cityidx;
                    }
                    GetCityBaseID(sub_city_id, cityidx);
                }
            }
            return cityidx;
        }
        public List<string> GetCityName(int cityid, List<string> cityname)
        {
            var modcity = citydao.GetCityBase(cityid);
            if (modcity != null)
            {
                if (modcity.city_id > 0)
                {
                    cityname.Insert(0, modcity.city_name.ToString());
                    int sub_city_id = (int)modcity.sub_city_id;
                    if (sub_city_id == 86)
                    {
                        return cityname;
                    }
                    GetCityName(sub_city_id, cityname);
                }
            }
            return cityname;
        }


    }
}
